<template>
  <div>
    <form class="form-signin">
      <div class="text-center mb-4">
        <img src="~/assets/login/image/login.png" alt="登录">
      </div>

      <div class="form-label-group">
        <input
          type="text"
          id="usernmae"
          v-model="passwordAndUsernameVO.username"
          placeholder="用户名"
          class="form-control"
          required
          autofocus
        >
        <label for="usernmae">用户名</label>
      </div>

      <div class="form-label-group">
        <input
          type="password"
          id="password"
          v-model="passwordAndUsernameVO.password"
          placeholder="密码"
          class="form-control"
          required
        >
        <label for="password">密码</label>
      </div>

      <div class="checkbox mb-3">
        <center>
          <el-row>
            <el-button type="primary" @click="handlerLogin()" plain>登陆</el-button>
            <nuxt-link to="/register">
              <el-button type="success" plain>注册</el-button>
            </nuxt-link>
          </el-row>
        </center>
      </div>
    </form>
  </div>
</template>
<style>

</style>

<script>
import "~/assets/login/css/floating-labels.css";
import authCodeApi from "@/api/authcode";
import userApi from "@/api/user";
import {getUser, setUser } from "@/utils/auth";

export default {
  layout: "login",
  data() {
    return {
      passwordAndUsernameVO: {} //实体
    };
  },
  methods: {
    handlerAuthCode() {
      authCodeApi.getAuthCode(this.pojo.username).then(response => {});
    },
    handlerLogin() {
      userApi.login(this.passwordAndUsernameVO).then(resp => {
        if (resp.data.flag) {
          
          //保存用户信息
          setUser(
            resp.data.rows.token,
            resp.data.rows.username,
            resp.data.rows.user_image,
            resp.data.rows.uid
          );

          //页面跳转
          location.href = "/manager";
        } else {
          this.$message({
            message: resp.data.msg,
            type: "error"
          });
          this.passwordAndUsernameVO.username = '';
          this.passwordAndUsernameVO.password = '';
        }
      });
    }
  },

  created() {
    if (getUser().username != undefined) {
      this.$message({
        message: "您已经登录!",
        type: "error"
      });
    }
  }
};
</script>

